package tech.yunland.shop.domain.service.apple;

import com.moyuer.bsf.domain.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import tech.yunland.activity.domain.imodel.BaseModel;
import tech.yunland.activity.domain.services.IBaseModelServices;
import tech.yunland.activity.domain.services.IBussinessServices;
import tech.yunland.shop.domain.dao.PurchaseBillDao;
import tech.yunland.shop.domain.dao.StoreBillDao;
import tech.yunland.shop.domain.model.PurchaseBill;
import tech.yunland.shop.domain.model.StoreBill;
import tech.yunland.shop.domain.service.PurchaseBillService;
import tech.yunland.shop.domain.service.StoreBillService;

import java.util.List;
import java.util.Map;

/**
 * @author 文卡<wkwenka@gmail.com>  on 17-3-29.
 */
@Service
public class BillAppleService implements IBussinessServices, IBaseModelServices<BaseModel> {

    @Autowired
    private PurchaseBillDao purchaseBillDao;

    @Autowired
    private PurchaseBillService purchaseBillService;

    @Autowired
    private StoreBillDao storeBillDao;

    @Autowired
    private StoreBillService storeBillService;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    protected LogService logService;

    @Override
    public List findLeaveBillList(String s) {
        return null;
    }

    @Override
    public void saveLeaveBill(Object o) {

    }

    @Override
    public void updateLeaveBill(Object o) {

    }

    @Override
    public void deleteLeaveBillById(String s) {

    }

    @Override
    public List getResultInfos(String s, String s1, int i, Integer integer) {
        return null;
    }

    @Override
    public BaseModel get(String s) {
        BaseModel baseModel;
        baseModel = purchaseBillDao.get(s);
        if (baseModel == null) {
            baseModel = storeBillDao.get(s);
        }
        return baseModel;
    }

    @Override
    public void update(Object o) {

    }

    @Override
    public Boolean isUsed(String id) {
        String sql1 = "select purchase_bill.id AS id,purchase_bill.sn AS sn,purchase_bill.processdef_id AS processdef_id from purchase_bill where purchase_bill.processdef_id='" + id + "'";
        String sql2 = "SELECT bill_store.id AS id,bill_store.sn AS sn,bill_store.processdef_id AS processdef_id  from bill_store WHERE bill_store.processdef_id='" + id + "'";

        String sql = sql1 + "UNION ALL " + sql2;
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        if (list != null && list.size() > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean save(BaseModel o) {

        if (o instanceof PurchaseBill) {
            purchaseBillDao.saveOrUpdate((PurchaseBill) o);
            if (((PurchaseBill) o).getState().intValue() == 2) {
                purchaseBillService.updatePurchaseBill(o.getId());
                logService.saveLog("采购单:Sn[" + ((PurchaseBill) o).getSn() + "] 审批通过。", ((PurchaseBill) o).getCreator());
            } else if (((PurchaseBill) o).getState().intValue() == -2) {
                logService.saveLog("采购单:Sn[" + ((PurchaseBill) o).getSn() + "] 审批不通过。", ((PurchaseBill) o).getCreator());
            }
            return true;
        } else if (o instanceof StoreBill) {
            storeBillDao.saveOrUpdate((StoreBill) o);
            Integer direction = ((StoreBill) o).getDirection();
            String s = direction < 0 ? "出库单" : "入库单";

            if (((StoreBill) o).getState().intValue() == 2) {
                storeBillService.updateGoodsStock((StoreBill) o, true, true);
                logService.saveLog(s + ":Sn[" + ((StoreBill) o).getSn() + "] 审批通过。", ((StoreBill) o).getCreator());
            } else if (((StoreBill) o).getState().intValue() == -2) {
                storeBillService.updateGoodsStock((StoreBill) o, true, false);
                logService.saveLog(s + ":Sn[" + ((StoreBill) o).getSn() + "] 审批不通过。", ((StoreBill) o).getCreator());
            }
            return true;
        }
        return false;
    }
}
